import { ref } from "vue";

export interface Notification {
  id: number;
  title: string;
  message: string;
  read: boolean;
}

class NotificationService {
  private notifications = ref<Notification[]>([]);
  private lastId = 0;

  getNotifications() {
    return this.notifications;
  }

  addNotification(title: string, message: string) {
    this.lastId++;
    this.notifications.value.push({
      id: this.lastId,
      title,
      message,
      read: false,
    });
  }

  markAsRead(id: number) {
    const notification = this.notifications.value.find((n) => n.id === id);
    if (notification) {
      notification.read = true;
    }
  }

  // 模拟从服务器获取新通知
  async pollForNotifications() {
    // 在实际应用中，这里应该是一个 API 调用
    const chance = Math.random();
    if (chance > 0.7) {
      this.addNotification("新消息", `这是一条模拟的新消息 ${Date.now()}`);
      return true;
    }
    return false;
  }
}

export const notificationService = new NotificationService();
